package com.geek.gkpush.service.order.event;

import com.geek.gkpush.dal.bean.EventMsgDO;
import com.geek.gkpush.dal.mapper.EventMsgMapper;
import com.geek.gkpush.framework.event.EventMsgPersistFacade;
import com.geek.gkpush.framework.event.enums.PushStatusEnum;
import com.geek.gkpush.framework.event.model.EventMsgDTO;
import com.geek.gkpush.framework.event.model.EventMsgRangeQueryModel;
import com.geek.gkpush.framework.event.model.EventMsgUpdateModel;
import com.geek.gkpush.util.copy.ShallowCopyUtils;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <h1>事件消息持久化接口实现类。</h1><br/>
 *
 * <h1><br/><a href="https://u.geekbang.org/subject/arch3/100614201">Java 业务架构实战营：https://u.geekbang.org/subject/arch3/100614201</a></h1><br/><h1><a href="https://gitee.com/ylimhhmily/BizArchPracticeCamp">Java 业务架构实战营代码地址：https://gitee.com/ylimhhmily/BizArchPracticeCamp</a></h1><br/><h1><a href="https://u.geekbang.org/subject/java4th/1001148?source=app_share">高级 Java 工程体系课 2023 版：https://u.geekbang.org/subject/java4th/1001148?source=app_share</a></h1><br/><h1><a href="https://time.geekbang.org/column/intro/100312101?tab=catalog">Dubbo 源码剖析与实战：https://time.geekbang.org/column/intro/100312101?tab=catalog</a></h1>
 *
 * @author hmilyylimh
 * ^_^
 * @version 0.0.1
 * ^_^
 * @date 2023/10/3 18:31
 */
@Component
public class EventMsgPersistFacadeImpl implements EventMsgPersistFacade {

    @Autowired
    private EventMsgMapper eventMsgMapper;

    @Override
    public void saveEventMsg(EventMsgDTO reqMsg) {
        EventMsgDO eventMsgDO = ShallowCopyUtils.copyBeanProperties(reqMsg, new EventMsgDO());
        eventMsgMapper.insertObject(eventMsgDO);
    }

    @Override
    public void batchSaveEventMsg(List<EventMsgDTO> reqMsgList) {
        List<EventMsgDO> reqList = ShallowCopyUtils.copyListBeanPropertiesToList(reqMsgList, new ArrayList<>(), EventMsgDO.class);
        eventMsgMapper.batchInsertObject(reqList);
    }

    @Override
    public int updatePushStatusByCondition(EventMsgUpdateModel model) {
        EventMsgDO eventMsg = new EventMsgDO();

        eventMsg.setId(model.getId());
        eventMsg.setBizId(model.getBizId());
        eventMsg.setBizType(model.getBizType());

        eventMsg.setPushStatus(model.getPushStatus());
        eventMsg.setPushResult(model.getPushResult());

        return eventMsgMapper.updatePushStatusByCondition(eventMsg, model.getSrcPushStatus());
    }

    @Override
    public EventMsgDTO queryObjectById(String id) {
        EventMsgDO dbEventMsgDO = eventMsgMapper.queryObjectById(id);
        return ShallowCopyUtils.copyBeanProperties(dbEventMsgDO, new EventMsgDTO());
    }

    @Override
    public void rangeUpdateList2Succ(EventMsgRangeQueryModel rangeModel) {
        // 先做查询动作，
        // 之所以要先查，为的避免一条 update 范围更新，会导致锁住别人的记录，进而引发死锁状况
        List<EventMsgDO> dbEventMsgList = eventMsgMapper.queryListByRangeTime(
                rangeModel.getBizType(),
                rangeModel.getBizId(),
                rangeModel.getSrcPushStatus(),
                rangeModel.getEndTimeMillis(),
                DateUtils.addHours(new Date(), -12).getTime()
        );
        if (CollectionUtils.isEmpty(dbEventMsgList)) {
            return;
        }

        // 最后最批量更新动作
        List<String> idList = dbEventMsgList.stream().map(EventMsgDO::getId).collect(Collectors.toList());
        eventMsgMapper.updateList2Succ(idList, rangeModel.getSrcPushStatus(), PushStatusEnum.SUCC.getMsg());
    }
}